package com.wyzz.hr.util.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.pdf.BaseFont;

public class HTML2PDF {

	public static void main(String[] args) {
		String fileNameWithPath = "c:\\1.pdf";
		String content = "";
		OutputStream os;
		try {
			FileInputStream fis = new FileInputStream("c:\\1.txt");
			InputStreamReader isr = new InputStreamReader(fis, "gbk");
			BufferedReader br = new BufferedReader(isr);
			String line = "";
			while ((line = br.readLine()) != null) {
				content += line;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
//		try {
//			content = new String(content.getBytes("utf-8"), "gbk");
//		} catch (UnsupportedEncodingException e1) {
//			e1.printStackTrace();
//		}
		System.out.println(content);
		try {
			os = new FileOutputStream(fileNameWithPath);
			ITextRenderer renderer = new ITextRenderer();
			ITextFontResolver fontResolver = renderer.getFontResolver();
			fontResolver.addFont("C:/Windows/Fonts/SIMSUN.TTC",
					BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
			renderer.setDocumentFromString(content);
			// String url = new File("c:\\3.txt").toURI().toURL().toString();
			// renderer.setDocument(url);
			renderer.layout();
			renderer.createPDF(os);
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
